#include "window_common.h"
#include "gkt_network.h"

#if defined(GKT_CONFIG_NETWORK_ENABLE) && GKT_CONFIG_NETWORK_ENABLE

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_DELAY_RETURN,
	TIMER_ID_UPGRADE_CANCEL_RESET,
	TIMER_ID_MAX
};

#define WINDOW_NETWORK_FIREWARE_UPGREDE_DELAY_RETURN_TIME_MS 2000

typedef struct _window_network_fireware_upgrade{
	uint32_t cancel_flag;
	uint32_t play_over;
}window_network_fireware_upgrade_s;
static window_network_fireware_upgrade_s s_window_network_fireware_upgrade;

static void window_firmware_upgrade_complete(window_result_e result)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;
	
	gkt_window_timer_clear(TIMER_ID_TIMEOUT);

	if (WINDOW_RESULT_OPERATE_SUCCESS == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_success), voice_flags, NULL);
	else if (WINDOW_RESULT_OPERATE_TIMEOUT == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_timeout), voice_flags, NULL);
	else if (WINDOW_RESULT_OPERATE_FAILED == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_failed), voice_flags, NULL);
	else
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_success), voice_flags, NULL);//operate cancel

	gkt_window_timeout_enable("window_firmware_upgrade - comlete");
	gkt_window_system_standby_enable();

	s_window_network_fireware_upgrade.play_over = 1;

}
static void window_firmware_upgrade_please_wait(void)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;
	gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(please_wait), voice_flags, NULL);
}
static int window_firmware_upgrade_event_handler(
				uint32_t major_type, uint32_t minor_type,
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_COMPLETE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_ACTIVE == minor_type) {
				s_window_network_fireware_upgrade.cancel_flag = 0;
				s_window_network_fireware_upgrade.play_over = 0;
				gkt_window_system_standby_disable();
				gkt_window_timeout_disable("window_firmware_upgrade - active");
				window_common_mask_all();
				if (gkt_network_is_connected()) {
					retval = gkt_network_firmware_upgrade();
					if ((GKT_SUCCESS == retval) || (GKT_EINPROCESS == retval)) {
						gkt_window_timer_set(TIMER_ID_DELAY_RETURN, 
							WINDOW_NETWORK_FIREWARE_UPGREDE_DELAY_RETURN_TIME_MS, 0, 0);
						gkt_window_timer_set(TIMER_ID_TIMEOUT, 
							GKT_NETWORK_FIRMWARE_UPGRADE_TIMEOUT_SEC*1000, 0, 0);
						window_firmware_upgrade_please_wait();
					}
					else
						window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_FAILED);
				}
				else {
					window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_FAILED);
				}
			}
			break;
		case GKT_EVENT_SYSTEM:
			if (GKT_WINDOW_SYSTEM_NETWORK == minor_type) {
				if (GKT_NETWORK_NOTIFY_TYPE_RUNNING_STATUS == id) {
					gkt_info("window_firmware_upgrade - (data:%d)\n", data);
					if (GKT_NETWORK_RUNNING_ST_UPGRADED == data)
						window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_SUCCESS);
					else if (GKT_NETWORK_RUNNING_ST_UPGRADE_FAILED == data)
						window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_FAILED);
					else if (GKT_NETWORK_RUNNING_ST_UPGRADE_CANCEL_FAILED == data)
						gkt_window_timer_set(TIMER_ID_UPGRADE_CANCEL_RESET, 1000, 0, 0);
					else if ((GKT_NETWORK_RUNNING_ST_UPGRADE_CANCEL_FORBID == data)
						|| (GKT_NETWORK_RUNNING_ST_UPGRADING == data))
					{
						window_common_mask_all();
					}
					else if (GKT_NETWORK_RUNNING_ST_UPGRADE_CANCELED == data) {
						gkt_window_timer_clear(TIMER_ID_UPGRADE_CANCEL_RESET);
						window_firmware_upgrade_complete(WINDOW_RESULT_MAX);
					}
				}
			}
		case GKT_EVENT_KEY:
			if (GKT_KEY_ASTERISK == minor_type) {
				if (!s_window_network_fireware_upgrade.cancel_flag) {
					retval = gkt_network_firmware_upgrade_cancel();
					if (GKT_SUCCESS == retval)
						s_window_network_fireware_upgrade.cancel_flag = 1;
					else {
						gkt_error("window_firmware_upgrade - cancel error. retval:%d\n", retval);
						s_window_network_fireware_upgrade.cancel_flag = 0;
					}
				}
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
		case GKT_EVENT_TIMER:
			if (TIMER_ID_TIMEOUT == minor_type)
				window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_TIMEOUT);
			else if (TIMER_ID_UPGRADE_CANCEL_RESET)
				s_window_network_fireware_upgrade.cancel_flag = 0;
			else if (TIMER_ID_DELAY_RETURN == minor_type)
				window_common_mask_except_return();
		case GKT_EVENT_VOICE:
			if (s_window_network_fireware_upgrade.play_over == 1) {
				s_window_network_fireware_upgrade.play_over = 0;
				gkt_window_exit();
			}
			break;
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE_NO_WIDGETS(firmware_upgrade);

#endif




